/** 
 * Project Name:sys-portal-Extjs 
 * File Name:MainCtrl.js 
 * Package Name: 
 * Author : 吴Yuan|小森森
 * Date:2018年4月24日下午12:56:17 
 * Copyright (c) 2018, 15909910367@163.com All Rights Reserved. 
 * 
 */
Ext.define('Jason.view.protal.module.systemManager.sources.dict.MainCtrl', {
    extend: 'Wys.basic.BaseCtrl',
    alias: 'controller.sources_dict_MainViewCtrl',
    /** private*/
    formItems: function () {
        return [{
            xtype: 'fieldset',
            title: '基本信息',
            collapsible: false,
            defaultType: 'textfield',
            layout: {
                type: 'table',
                columns: 1
            },
            defaults: {
                width: '100%',
                labelWidth: 100
            },
            items: [
                { fieldLabel: '字典名称', name: 'typename', xtype: 'textfield', flex: 1, anchor: '100%', readOnly: true, allowBlank: false, afterLabelTextTpl: cfg.required },
                { fieldLabel: '数据名称', name: 'key', xtype: 'textfield', flex: 1, anchor: '100%', allowBlank: false, afterLabelTextTpl: cfg.required },
                { fieldLabel: '数据值', name: 'value', xtype: 'textfield', flex: 1, anchor: '100%', allowBlank: false, afterLabelTextTpl: cfg.required },
                { fieldLabel: '父类', xtype: 'hiddenfield', name: 'parent' },
                { fieldLabel: '父类类型', xtype: 'hiddenfield', name: 'type' },
                { fieldLabel: '状态', xtype: 'hiddenfield', name: 'state', value: 1 },
                { fieldLabel: '排序', value: 1,maxValue:99, name: 'order', xtype: 'numberfield', flex: 1, anchor: '100%', allowBlank: false, afterLabelTextTpl: cfg.required },
                { fieldLabel: '备注', name: 'remarks', flex: 1, xtype: 'textareafield', grow: true, anchor: '100%' }
            ]
        }];
    },
    /** private*/
    formItemsByParent: function () {
        return [{
            xtype: 'fieldset',
            title: '基本信息',
            collapsible: false,
            defaultType: 'textfield',
            layout: {
                type: 'table',
                columns: 1
            },
            defaults: {
                width: '100%',
                labelWidth: 100
            },
            items: [
                { name: 'id', xtype: 'hiddenfield' },
                { fieldLabel: '类别名称', name: 'key', xtype: 'textfield', flex: 1, anchor: '100%', allowBlank: false, afterLabelTextTpl: cfg.required },
                {
                    fieldLabel: '类别代码', name: 'type', xtype: 'textfield', flex: 1, anchor: '100%', allowBlank: false, afterLabelTextTpl: cfg.required,
                    emptyText: '用于查询时快速检索'
                },
                { fieldLabel: '父类', xtype: 'hiddenfield', name: 'parent', value: -1 },
                { fieldLabel: '状态', xtype: 'hiddenfield', name: 'state', value: 1 },
                {
                    xtype: 'radiogroup',
                    fieldLabel: '状态',
                    vertical: true,
                    simpleValue: true,
                    bind: '{state}',
                    layout: 'hbox',
                    items: [
                        {
                            boxLabel: '启用',
                            checked: true,
                            name: 'state',
                            inputValue: 1
                        }, {
                            boxLabel: '停用',
                            name: 'state',
                            inputValue: 2
                        }
                    ]
                },
                { fieldLabel: '备注', name: 'remarks', flex: 1, xtype: 'textareafield', grow: true, anchor: '100%' }
            ]
        }];
    },
    addDictParent: function (btn) {//添加字典父级
        var me = this;
        Ext.create('Wys.form.FormWindow', {
            width: 524,
            listeners: {
                hide: function () {
                    me.getView().down('#dictListTree').getStore().reload();
                }
            },
            title: '字典类别',
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemManager.souces.dict.DictModel',
                gridPanel: me.getView().down('#dictMainView'),
                items: me.formItemsByParent()
            }
        }).show();
    },
    editDictParent: function () {
        var me = this,
            treeView = me.getView().down('#dictListTree'),
            treeNodeSelected = treeView.getSelectionModel().getSelection();
        if (treeNodeSelected.length === 0) {
            EUtil.showWarning('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
            return;
        }
        EUtil.RS({
            url: reqUrl.sys.dict.getOne,
            method: 'GET',
            params: {
                code: treeNodeSelected[0].data.id
            },
            callback: function (resp) {
                var record = Ext.create('Jason.model.protal.module.systemManager.souces.dict.DictModel', resp);
                Ext.create('Wys.form.BaseWindow', {
                    width: 524,
                    listeners: {
                        hide: function () {
                            me.getView().down('#dictListTree').getStore().reload();
                        },
                        show: function (win) {
                            win.down('baseForm').loadRecord(record);
                        }
                    },
                    title: '修改字典类别【' + treeNodeSelected[0].data.text + '】',
                    iconCls: 'fa fa-edit',
                    modal: true,
                    items: [{
                        xtype: 'baseForm',
                        items: me.formItemsByParent(),
                        buttons: [{
                            text: '保存',
                            iconCls: 'x-fa fa-floppy-o',
                            handler: function (button) {
                                var form = button.up('baseForm').getForm();
                                formValues = form.getValues();
                                EUtil.RS({
                                    url: reqUrl.sys.dict.update,
                                    params: formValues,
                                    callback: function (resp) {
                                        if (resp.extResultUtil.success) {
                                            button.up("window").close();
                                        } else {
                                            EUtil.showError('修改字典类别【' + treeNodeSelected[0].data.text + '】失败');
                                        }
                                    }
                                });
                            }
                        }, {
                            text: '关闭',
                            itemId: 'close',
                            iconCls: 'x-fa fa-close',
                            handler: function (button) {
                                this.up("window").close();
                            }
                        }]
                    }]
                }).show();
            }
        });
    },
    onClickDictParentTreeItem: function (treepanel, record, item, index, e, eOpts) {
        var me = this,
            grid = me.getView().down('#dictMainView');
        grid.dictParentCode = record.get('id');
        grid.dictParentType = record.get('type');
        grid.dictParentTypeName = record.get('text');
        grid.load({
            parent: record.get('id')
        });
    },
    onNewButtonClick: function (btn) {
        var me = this;
        grid = me.getView().down('#dictMainView');
        if (!grid.dictParentCode) {
            EUtil.showError('请先选择字典类型');
            return;
        }
        Ext.create('Wys.form.FormWindow', {
            width: 524,
            title: '数据字典',
            listeners: {
                show: function (win) {
                    win.down('hiddenfield[name=parent]').setValue(grid.dictParentCode);
                    win.down('hiddenfield[name=type]').setValue(grid.dictParentType);
                    win.down('textfield[name=typename]').setValue(grid.dictParentTypeName);
                }
            },
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemManager.souces.dict.DictModel',
                gridPanel: grid,
                items: me.formItems()
            }
        }).show();
    },
    onPreviewButtonClick: function (grid, record) {
        var me = this;
        var grid = this.getView().down('#dictMainView'),
            records = grid.getSelection();
        if (records.length === 0 || records.length > 1) {
            EUtil.showWarning('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
            return;
        } else {
            record = records[0];
        }
        Ext.create('Wys.form.FormWindow', {
            width: 524,
            title: ' 『' + record.get('typename') + '』数据字典信息',
            config: {
                model: 'Jason.model.protal.module.systemManager.souces.dict.DictModel',
                operatetype: 'display',
                gridPanel: grid,
                items: me.formItems(),
                formData: record
            }
        }).show();
    },
    onEditButtonClick: function (grid, record) {
        var me = this;
        var grid = this.getView().down('#dictMainView'),
            records = grid.getSelection();
        if (records.length === 0 || records.length > 1) {
            EUtil.showWarning('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
            return;
        } else {
            record = records[0];
        }
        Ext.create('Wys.form.FormWindow', {
            width: 524,
            title: ' 『' + record.get('typename') + '』数据字典信息',
            config: {
                model: 'Jason.model.protal.module.systemManager.souces.dict.DictModel',
                operatetype: 'edit',
                gridPanel: grid,
                items: me.formItems(),
                formData: record
            }
        }).show();
    },
    onDeleteButtonClick: function (btn) {
        var grid = this.getView().down('#dictMainView'),//btn.up('#sysManagerSoucesMenuMainView'),
            gridStore = grid.getStore(),
            selectGridData = grid.getSelection(),
            removePK = [],
            removeName = [];
        if (selectGridData.length === 0) {
            EUtil.toastWarn('请选择要操作的数据');
            return;
        }
        Ext.Array.each(selectGridData, function (item) {
            removeName.push(item.data.key);
            removePK.push(item.data.id);
        });
        EUtil.showMsg({
            option: 3,
            title: '温馨提示',
            message: '您确定删除当前选中字典为<span style="color:red;">『' + removeName.join() + '』</span>吗？',
            callback: function (btn) {
                if (btn === 'ok') {
                    gridStore.remove(selectGridData);
                    gridStore.sync({
                        callback: function () {
                            gridStore.reload();
                        }
                    });
                }
            }
        });
    },
    onGridCommand: function (item, command, record, recordIndex, cellIndex) {
        var me = this,
            grid = item.gridRef;
        if (command === 'modify') {/** 修改 */
            grid.setSelection(record);
            grid.clearSelection();
            Ext.create('Wys.form.FormWindow', {
                width: 524,
                title: '数据字典',
                config: {
                    operatetype: 'edit',
                    model: 'Jason.model.protal.module.systemManager.souces.dict.DictModel',
                    defaults: {
                        anchor: '100%',
                        labelWidth: 80,
                        labelSeparator: '：'
                    },
                    gridPanel: me.getView().down('#dictMainView'),
                    formData: record,
                    items: me.formItems()
                }
            }).show();
        }
        if (command === 'preview') {
            grid.setSelection(record);
            grid.clearSelection();
            Ext.create('Wys.form.FormWindow', {
                width: 524,
                title: '数据字典',
                config: {
                    operatetype: 'display',
                    defaults: {
                        anchor: '100%',
                        labelWidth: 80,
                        labelSeparator: '：'
                    },
                    model: 'Jason.model.protal.module.systemManager.souces.dict.DictModel',
                    gridPanel: item,
                    formData: record,
                    items: me.formItems()
                }
            }).show();
        }
        if (command === 'remove') {/** 删除*/
            grid.setSelection(record);
            grid.clearSelection();
            EUtil.showMsg({
                option: 3,
                title: '温馨提示',
                message: '您确定删除<span style="color:red;">『' + record.data.typename + '』</span>吗？',
                callback: function (btn) {
                    if (btn === 'ok') {
                        grid.getStore().remove(record);
                        grid.getStore().sync({
                            callback: function () {
                                grid.load();
                            }
                        });
                    }
                }
            });
        }
        if (command == 'addChildren') {
            var grid = me.getView().down('#dictMainView');
            Ext.create('Wys.form.FormWindow', {
                width: 524,
                title: '数据字典',
                listeners: {
                    show: function (win) {
                        win.down('hiddenfield[name=parent]').setValue(record.get('id'));
                        win.down('hiddenfield[name=type]').setValue(grid.dictParentType);
                        win.down('textfield[name=typename]').setValue(record.get('typename'));
                    }
                },
                config: {
                    operatetype: 'new',
                    model: 'Jason.model.protal.module.systemManager.souces.dict.DictModel',
                    gridPanel: grid,
                    items: me.formItems()
                }
            }).show();
        }
    },
    onGridRepareCommands: function (self, gridRef, commands, record, row) {
        //这里可以去控制操作按钮的显示隐藏
    }
});